package com.cat.chapter050;

import java.util.Arrays;

/**
 * @author cat
 * @description https://leetcode.cn/problems/boats-to-save-people/
 * @create 2025/7/21 10:24
 * @since JDK17
 */

public class Solution03 {
    public int numRescueBoats(int[] people, int limit) {
        Arrays.sort(people);    // 排升序
        int ans = 0, n = people.length;
        for (int l = 0, r = n - 1; l < r; ans++) {
            if (people[l] + people[r] <= limit) {
                l++;
                r--;
            } else {
                r--;
            }
        }
        return ans;
    }
}
